<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>单位管理</el-breadcrumb-item>
      <el-breadcrumb-item>单位列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->

    <el-card>
      <!-- 搜索与添加 -->
      <el-row>
        <el-col :span="7">
          <el-input placeholder="请输入名字" v-model="queryInfo.query" clearable @clear="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button @click="addDiglogVisible=true">添加</el-button>
          <el-button>导入</el-button>
          <el-button>导出</el-button>
          <!-- <el-button @click="">统计</el-button> -->
        </el-col>
      </el-row>

      <!-- 用户展示 -->
      <el-table
    :data="userList"
    
    border
    :default-sort = "{prop: 'index', order: 'descending'}"
    >
     
        <el-table-column type="index" sortable></el-table-column>
        <el-table-column prop="orgName" label="名称" sortable></el-table-column>
        <el-table-column prop="orgType" label="性质" sortable>
          <template slot-scope="scope" >
            <p v-if="scope.row.orgType===1">协会</p>
            <p v-if="scope.row.orgType===2">学校</p>
              <p v-if="scope.row.orgType===3">企业</p>
            <p v-if="scope.row.orgType===null"></p>
          </template>
        </el-table-column>
        <el-table-column prop="orgCity" label="城市" sortable></el-table-column>
         <el-table-column prop="orgSn" label="会员号" sortable></el-table-column>
        <!-- 状态区域 -->
        <el-table-column prop="orgState" label="状态" sortable>
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.orgState"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="userStateChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <!-- 操作区域 -->
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <!-- 查看会员 -->
            <el-tooltip
             
              class="item"
              effect="dark"
              content="查看单位会员"
              placement="top"
              :enterable="false"
            >
             <el-button type="primary" size="mini" icon="el-icon-user" @click="getThisOrgUser(scope.row.id)"></el-button>
            </el-tooltip>
            <!-- 修改 -->
              <el-tooltip
             
              class="item"
              effect="dark"
              content="修改单位"
              placement="top"
              :enterable="false"
            >
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="showEditDialog(scope.row.id)"></el-button>
             </el-tooltip>
            <!-- 删除 -->
    <el-popconfirm
  title="您确定删除吗？"
     	confirmButtonText='好的'
     	cancelButtonText="取消"
  @onConfirm="deletedUser(scope.row.id)"
>
  
            <el-button type="danger" size="mini" icon="el-icon-delete" slot="reference" ></el-button>
         </el-popconfirm>
            <!-- 重置密码 -->
            <!-- <el-button
            size="mini"
              type="warning"
              icon="el-icon-refresh-left"
              @click="restPassWord(scope.row.userId)"
            ></el-button> -->
            <!-- 发消息 -->
            <!-- <el-button type="info" icon="el-icon-message" size="mini" @click="messageDialogVisible=true"></el-button> -->
            <!-- 角色按钮 -->
            <!-- <el-tooltip
             
              class="item"
              effect="dark"
              content="分配角色"
              placement="top"
              :enterable="false"
            >
              <el-button type="warning" icon="el-icon-cpu" size="mini"></el-button>
            </el-tooltip> -->
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[10, 50, 100, 200]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      <!-- 添加用户 -->
      <el-dialog title="添加用户" :visible.sync="addDiglogVisible" width="50%" @close="addUserClosed">
        <!-- 内容主体区 -->
        <el-form
          :model="addUserForm"
          :rules="addUserFormRules"
          ref="addUserFormRef"
          label-width="100px"
        >
          <el-form-item label="名称" prop="orgName">
            <el-input v-model="addUserForm.orgName"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="orgAddress">
            <el-input v-model="addUserForm.orgAddress"></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="addUserForm.type" placeholder="类型">
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.typeName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <!-- 增加用户底部区域 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDiglogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUser">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 修改用户 -->
      <el-dialog
  title="修改用户"
  :visible.sync="editUserDialogVisible"
  width="50%"
  >
  <el-form ref="editUserFormRef" :model="editUserForm"  :rules="editUserFormRules" label-width="80px">
  <el-form-item label="单位名称">
    <el-input v-model="editUserForm.orgName"></el-input>
  </el-form-item>
 
  <el-form-item label="地址">
    <el-input v-model="editUserForm.orgAddress"></el-input>
  </el-form-item>
  </el-form>
  


 <!-- 修改用户底部区域 -->
  <span slot="footer" class="dialog-footer">
    <el-button @click="editUserDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editUserDialogVisible = false">确 定</el-button>
  </span>
</el-dialog>
<!-- 查看该单位会员区域 -->
  <el-dialog
  title="该单位会员列表"
  :visible.sync="showUserFromThisDeptDialogVisible"
  width="50%"
  >
  <el-table
     :data="thisTeamList"
      style="width: 100%">
      <el-table-column
        prop="userName"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="userSnNumber"
        label="会员号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="userAge"
        label="年龄">
      </el-table-column>
    </el-table>
  


 
</el-dialog>



      <!-- 删除用户 -->

      <!-- 给用户发私信 -->
      <el-dialog
  title="写消息"
  :visible.sync="messageDialogVisible"
  width="30%"
  >
  <el-form ref="form"  label-width="80px">
  
    <el-input
  type="textarea"
  :rows="4"
  placeholder="请输入内容"
  >
</el-input>
  
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="messageDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="messageDialogVisible = false">确 定</el-button>
  </span>
</el-dialog>
      <!-- 分配角色 -->
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      visible: false,
      //后区用户列表的参数
      queryInfo: {
        query: "",
        //当前的页数
        pageNum: 1,
        pageSize: 10
      },
      thisTeamList:[],
      userList: [],
      total: 0,
      //控制添加用户对话框的显示与隐藏
      addDiglogVisible: false,
      //控制删除用户对话框的显示与隐藏
      deleteUserDialogVisible: false,
      //控制修改用户对话框的显示与隐藏
      editUserDialogVisible:false,
      //控制消息对话框的显示与隐藏
      messageDialogVisible:false,
      showUserFromThisDeptDialogVisible:false,
      //添加用户表单
      addUserForm: {
        userName: "",
        userCertnumber: "",
        userDeptId: ""
      },
      //添加表单的验证规则
      addUserFormRules: {
        userName: [
          {
            required: true,
            message: "请输入姓名",
            //失去焦点时触发
            trigger: "blur"
          },
          {
            min: 3,
            max: 10,
            message: "用户名的长度在3-10个字符之间",
            trigger: "blur"
          }
        ]
      },
      //会员单位类型
      typeList: [
        {id:1,
typeName:'协会',
        },
         {id:2,
typeName:'学校',
        },
         {id:3,
typeName:'俱乐部',
        }
      ],
      //修改用户表单
      editUserForm: {
        userName: "xxx",
        userCertnumber: "",
        userDeptId: ""
      },
editUserFormRules: {
        userName: [
          {
            required: true,
            message: "请输入姓名",
            //失去焦点时触发
            trigger: "blur"
          },
          {
            min: 3,
            max: 10,
            message: "用户名的长度在3-10个字符之间",
            trigger: "blur"
          }
        ]
      },





    };
  },
  created() {
    this.getUserList();
    
  },
  methods: {
    async getUserList() {
      const { data: res } = await this.$http.get("organization/getOrganizations", {
        params: this.queryInfo
      });
      if (res.code !== 200) {
        return this.$message.error("获取用户列表失败！");
      }

      this.userList = res.data.records;
      this.total = res.data.total;
    },
    //监听页码容量的改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getUserList();
    },
    //获取组织机构
    async getOrgList() {
      const { data: res } = await this.$http.get("/organization/getOrgList");
      if (res.code !== 200) return this.$message.error(res.msg);
      console.log(res.data);
      this.orgList = res.data;
    },
    //监听页码值改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getUserList();
    },
    //状态改变
    async userStateChange(userInfo) {
      console.log(userInfo)
      const { data: res } = await this.$http.put(
        `organization/updateOrgState?userId=${userInfo.id}&state=${userInfo.orgState}`
      );

      if (res.code !== 200) {
        this.$message.error("获取用户列表失败！");
      } else if (res.code === 200) {
        this.$message.success("状态修改成功！");
      }
    },
    //监听添加用户对话框关闭事件
    addUserClosed() {
      this.$refs.addUserFormRef.resetFields();
    },
    //点击按钮添加新用户
    addUser() {
      this.$refs.addUserFormRef.validate(async valid => {
        if (!valid) return;
        //可以发起添加用户的网络请求
        const { data: res } = await this.$http.put(
          `organization/saveOrg?orgName=${this.addUserForm.orgName}&orgAddress=${this.addUserForm.orgAddress}&type=${this.addUserForm.type}&city=${this.addUserForm.city}`
        );
        if (res.code !== 200) {
          this.$message.error("获取用户列表失败！");
        } else if (res.code === 200) {
          this.$message.success("添加成功！");
          this.addDiglogVisible = false;
          this.getUserList();
        }
      });
    },
    //删除用户
    async deletedUser(userID) {
      console.log(userID)
      const { data: res } = await this.$http.put(
        `organization/deleteUser?userId=${userID}`
      );

      if (res.code !== 200) {
        this.$message.error("修改失败！");
      } else if (res.code === 200) {
        this.$message.success("删除成功！");
      }
      this.getUserList();
    },

    //重置密码
    async restPassWord(userID) {
      const { data: res } = await this.$http.put(
        `user/restPassWord?userId=${userID}`
      );

      if (res.code !== 200) {
        this.$message.error("修改失败！");
      } else if (res.code === 200) {
        this.$message.success("密码重置成功！");
      }
    },
    //展示用户编辑的对话框
   async showEditDialog(id){
this.editUserDialogVisible=true
const {data:res}=await this.$http.get(`organization/getById?userId=${id}`)
 if (res.code !== 200) {
        this.$message.error("请求失败！");
      } 
      console.log(res.data)
this.editUserForm=res.data
this.editUserDialogVisible=true

    },
    //展示该单位所有用户
 async getThisOrgUser(id){
const {data:res}=await this.$http.get(`user/getThisOrgUser?userDeptId=${id}`)
this.showUserFromThisDeptDialogVisible=true
 if (res.code !== 200) {
        this.$message.error("请求失败！");
      } 
      console.log(res.data)
this.thisTeamList=res.data
this.showUserFromThisDeptDialogVisible=true
 }
  }
};
</script>
<style lang="less" scoped>
.el-card {
  justify-content: space-between;
}
.el-pagination {
  margin-top: 15px;
}
.el-dialog .el-input {
  width: 50%;
}
.el-main ,.el-table{
  line-height: 0;
}
</style>